package com.company.oa.announcement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.oa.announcement.dto.TemplateDTO;
import com.company.oa.announcement.entity.AnnouncementTemplate;
import com.company.oa.announcement.mapper.AnnouncementTemplateMapper;
import com.company.oa.announcement.service.AnnouncementTemplateService;
import com.company.oa.common.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class AnnouncementTemplateServiceImpl extends ServiceImpl<AnnouncementTemplateMapper, AnnouncementTemplate> implements AnnouncementTemplateService {

    @Override
    public IPage<AnnouncementTemplate> getTemplatePage(int page, int size, AnnouncementTemplate template) {
        Page<AnnouncementTemplate> pageParam = new Page<>(page, size);
        
        LambdaQueryWrapper<AnnouncementTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnnouncementTemplate::getIsDeleted, 0);
        
        if (template != null) {
            if (template.getName() != null && !template.getName().isEmpty()) {
                wrapper.like(AnnouncementTemplate::getName, template.getName());
            }
            
            if (template.getType() != null) {
                wrapper.eq(AnnouncementTemplate::getType, template.getType());
            }
            
            if (template.getIsSystem() != null) {
                wrapper.eq(AnnouncementTemplate::getIsSystem, template.getIsSystem());
            }
        }
        
        wrapper.orderByDesc(AnnouncementTemplate::getCreateTime);
        
        return page(pageParam, wrapper);
    }

    @Override
    public boolean createTemplate(TemplateDTO templateDTO, Long userId, String userName) {
        AnnouncementTemplate template = new AnnouncementTemplate();
        BeanUtils.copyProperties(templateDTO, template);
        
        template.setCreatorId(userId);
        template.setCreatorName(userName);
        template.setCreateTime(LocalDateTime.now());
        template.setIsSystem(0); // 用户创建的模板
        template.setIsDeleted(0);
        
        return save(template);
    }

    @Override
    public boolean updateTemplate(TemplateDTO templateDTO) {
        // 检查模板是否存在
        AnnouncementTemplate existTemplate = getById(templateDTO.getId());
        if (existTemplate == null) {
            throw new BusinessException("模板不存在");
        }
        
        // 系统模板不允许修改
        if (existTemplate.getIsSystem() == 1) {
            throw new BusinessException("系统模板不允许修改");
        }
        
        AnnouncementTemplate template = new AnnouncementTemplate();
        BeanUtils.copyProperties(templateDTO, template);
        template.setUpdateTime(LocalDateTime.now());
        
        return updateById(template);
    }

    @Override
    public boolean deleteTemplate(Long id) {
        // 检查模板是否存在
        AnnouncementTemplate existTemplate = getById(id);
        if (existTemplate == null) {
            throw new BusinessException("模板不存在");
        }
        
        // 系统模板不允许删除
        if (existTemplate.getIsSystem() == 1) {
            throw new BusinessException("系统模板不允许删除");
        }
        
        // 逻辑删除
        AnnouncementTemplate template = new AnnouncementTemplate();
        template.setId(id);
        template.setIsDeleted(1);
        template.setUpdateTime(LocalDateTime.now());
        
        return updateById(template);
    }
} 